package com.sc.study.foundation.service.impl;

import java.io.IOException;
import java.util.List;

import com.sc.study.exam.domain.ScQuestion;
import com.sc.study.exam.mapper.ScQuestionMapper;
import com.sc.study.foundation.domain.ScSubjectChapter;
import com.sc.study.foundation.mapper.ScGradeMapper;
import com.sc.study.foundation.mapper.ScSubjectChapterMapper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sc.study.foundation.mapper.ScSubjectMapper;
import com.sc.study.foundation.domain.ScSubject;
import com.sc.study.foundation.service.IScSubjectService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * 课程Service业务层处理
 *
 * @author sc
 * @date 2022-02-27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ScSubjectServiceImpl implements IScSubjectService {
    @Autowired
    private ScSubjectMapper scSubjectMapper;
    @Autowired
    private ScGradeMapper scGradeMapper;

    @Autowired
    private ScQuestionMapper scQuestionMapper;

    @Autowired
    private ScSubjectChapterMapper scSubjectChapterMapper;



    /**
     * 查询课程
     *
     * @param subid 课程主键
     * @return 课程
     */
    @Override
    public ScSubject selectScSubjectBySubid(Long subid) {
        return scSubjectMapper.selectScSubjectBySubid(subid);
    }

    /**
     * 查询课程列表包含年级
     *
     * @param scSubject 课程
     * @return 课程
     */
    @Override
    public List<ScSubject> selectScSubjectList(ScSubject scSubject) {
        return scSubjectMapper.selectSubjectIncludeGrade(scSubject);
    }

    /**
     * 查询课程列表包含年级和老师
     *
     * @param scSubject 课程
     * @return 课程
     */
    @Override
    public List<ScSubject> selectSubjectIncludeGradeAndUsers(ScSubject scSubject) {
        return scSubjectMapper.selectSubjectIncludeGradeAndUsers(scSubject);
    }


    /**
     * 新增课程
     *
     * @param scSubject 课程
     * @return 结果
     */
    @Override
    public int insertScSubject(ScSubject scSubject) {
        return scSubjectMapper.insertScSubject(scSubject);
    }

    /**
     * 修改课程
     *
     * @param scSubject 课程
     * @return 结果
     */
    @Override
    public int updateScSubject(ScSubject scSubject) {
        return scSubjectMapper.updateScSubject(scSubject);
    }

    /**
     * 批量删除课程
     *
     * @param subids 需要删除的课程主键
     * @return 结果
     */
    @Override
    public int deleteScSubjectBySubids(Long[] subids) {

        return scSubjectMapper.deleteScSubjectBySubids(subids);
    }

    /**
     * 删除课程信息
     *
     * @param subid 课程主键
     * @return 结果
     */
    @Override
    public int deleteScSubjectBySubid(Long subid) {
        return scSubjectMapper.deleteScSubjectBySubid(subid);
    }

    /**
     * 查询所有下拉显示：
     * 带出属性 选择题的题目对象
     *
     * @return
     */
    @Override
    public List<ScSubject> listsubjectIncludeQuestions() {
        List<ScSubject> scSubjects = scSubjectMapper.selectScSubjectList(null);

        //查询条件
        ScQuestion scQuestion = new ScQuestion();
        scQuestion.setQuestiontypeid(2L);

        //带出属性 选择题目集合
        scSubjects.forEach(item -> {
            scQuestion.setSubjectid(item.getSubid());
            item.setScQuestionByOption(scQuestionMapper.selectScQuestionList(scQuestion));
        });

        return scSubjects;
    }


    /**
     * 导入Excel
     *
     * @param file Excel
     * @throws IOException
     */
    @Override
    public void importExcel(MultipartFile file) throws IOException {
        //将传入进来的文件转化为工作簿
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        //获取excel sheet页面
        Sheet sheet = workbook.getSheetAt(0);
        //获取当前sheet页行数
        int rows = sheet.getPhysicalNumberOfRows();
        // 课程的信息
        Row sheetRow = sheet.getRow(2);
        ScSubject scSubject = new ScSubject();

        //课程名称
        scSubject.setSubname(sheetRow.getCell(0).toString());

        //年级
        scSubject.setGradeid((long) ((int) sheetRow.getCell(1).getNumericCellValue()));

        //每次消耗课时
        scSubject.setSubattime((long) ((int) sheetRow.getCell(2).getNumericCellValue()));

        //每课时费用
        scSubject.setSubtimemoney((long) ((int) sheetRow.getCell(3).getNumericCellValue()));

        //老师每课时费用
        scSubject.setSubtimeteachermoney((long) ((int) sheetRow.getCell(4).getNumericCellValue()));

        //每周多少次课
        scSubject.setSubweeknum((long) ((int) sheetRow.getCell(5).getNumericCellValue()));

        //不连续多少次
        scSubject.setSubcontinue((long) ((int) sheetRow.getCell(6).getNumericCellValue()));

        //添加一个课程
        scSubjectMapper.insertScSubject(scSubject);
        long totalTime=0L;
        for (int i = 5; i < rows; i++) {
            Row row = sheet.getRow(i);
            //章节名称
            String name = row.getCell(0).toString();
            //顺序
            Long order = (long) ((int) row.getCell(1).getNumericCellValue());
            //章节课时
            Long chapterTime = (long) ((int) row.getCell(2).getNumericCellValue());

            ScSubjectChapter scSubjectChapter=new ScSubjectChapter();

            scSubjectChapter.setName(name);
            scSubjectChapter.setOrder(order);
            scSubjectChapter.setSubjectid(scSubject.getSubid());
            scSubjectChapter.setChaptertime(chapterTime);

            totalTime=totalTime+chapterTime;

            //添加一个章节
            scSubjectChapterMapper.insertSubjectChapter(scSubjectChapter);
        }
        //将课程的总课时更改
        scSubject.setSubtime(totalTime);
        scSubjectMapper.updateScSubject(scSubject);
    }
}
